//
//  XMTabBarCtr.m
//  TestView
//
//  Created by lk@wxm on 2024/12/3.
//

#import "XMTabBarCtr.h"
#import "XMFirstVC.h"
#import "XMNavCtr.h"
@interface XMTabBarCtr ()

@end

@implementation XMTabBarCtr

#pragma mark - 关于旋转的设置


//支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations];
}

//默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTabBar];
    self.tabBar.itemPositioning = UITabBarItemPositioningFill;
    [[UITabBar appearance] setBackgroundColor:KWhiteColor];
    if (@available(iOS 13, *)) {
//#warning ios13
        UIUserInterfaceStyle mode = UITraitCollection.currentTraitCollection.userInterfaceStyle;
        if (mode == UIUserInterfaceStyleDark) {
            NSLog(@"深色模式");
            self.tabBar.unselectedItemTintColor = [UIColor whiteColor]; //未选中字体的颜色
            self.tabBar.tintColor =  [UIColor whiteColor]; //选中字体的颜色
        } else if (mode == UIUserInterfaceStyleLight) {
            NSLog(@"浅色模式");
        } else {
            NSLog(@"未知模式");
        }
    } else {
        // Fallback on earlier versions
    }
    // Do any additional setup after loading the view.
}
- (void)createTabBar{
    //需要
    [self createControllerWithTitle:@"首页" image:@"menu-message-normal"selectedimage:@"menu-message-down" className:[XMFirstVC class]];
//     [self createControllerWithTitle:@"成长" image:@"menu-contact-normal"selectedimage:@"menu-contact-down" className:[MPSecondCtr class]];
//     [self createControllerWithTitle:@"我的" image:@"menu-more-normal"selectedimage:@"menu-more-down" className:[MPThirdCtr class]];
//     [MPNavigator sharedMPNavigator].mainNav = self.childViewControllers[0];
}

//提取公共方法
- (void)createControllerWithTitle:(NSString *)title image:(NSString *)image selectedimage:(NSString *)selectedimage  className:(Class)class{
    UIViewController *vc = [[class alloc]init];
    XMNavCtr *nav = [[XMNavCtr alloc]initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedimage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
    nav.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:18.0/255 green:183.0/255 blue:245.0/255 alpha:1.0], NSForegroundColorAttributeName,
                                                       nil,nil] forState:UIControlStateSelected];
}

// 点击tabbarItem自动调用
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//    if([item.title isEqualToString:@"发现"])
//    {
//        // 也可以判断标题,然后做自己想做的事
//    }
    
}
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray <UIView *> *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    /**
     CABasicAnimation类的使用方式就是基本的关键帧动画。
     
     所谓关键帧动画，就是将Layer的属性作为KeyPath来注册，指定动画的起始帧和结束帧，然后自动计算和实现中间的过渡动画的一种动画方式。
     */
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.9];
    pulse.toValue= [NSNumber numberWithFloat:1.1];
    [[tabbarbuttonArray[index] layer]addAnimation:pulse forKey:nil];
    
}
@end
